package com.baidu.controller;

import com.baidu.pojo.*;
import com.baidu.service.OrdersService;
import com.baidu.service.UserService;
import com.baidu.utils.Pages;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;

    //    买票
    @RequestMapping("/buyTicket")
    @ResponseBody
    public ReturnJson buyTicket(Ticket ticket,HttpSession session,Integer number){
        ReturnJson rj = new ReturnJson();
        User u =(User) session.getAttribute("user1");
//        先减钱，再减票，再插入
        u.setUcount((Integer)u.getUcount()-(Integer) ticket.getTmoney()*number);
        Integer max = u.getUcount();
        if (max>0) {
            ordersService.updateUcount(u);
        }else{
            rj.setCode(201);rj.setMsg("余额不足");return rj;
        }
        ticket.setTnum(ticket.getTnum()-number);
        if (ticket.getTnum()>0) {
            ordersService.updateTNum(ticket);
        }else{
            rj.setCode(201);rj.setMsg("余票不足");return rj;
        }
        Order order = new Order();
        order.setUid(u.getUid());
        order.setTid(ticket.getTid());
        Date date =new Date();
        order.setOtime(date);
        order.setOquantity(number);
        ordersService.addOrder(order);
        rj.setCode(200);
        rj.setMsg("下单成功");
        session.setAttribute("user1",userService.getUserById(u.getUid()));
        return rj;
    }
    @RequestMapping("/goOK")
    public String goOk(){
        System.out.println("GOGOGO");
        return "home";
    }
    //    订单
    @RequestMapping("/getAllOrderByUId")
    public String getAllOrderByUId(HttpSession session){
        System.out.println("getAllOrderByUId");
        User user =(User) session.getAttribute("user1");
        List<Order> or = ordersService.getAllOrderByUId(user.getUid());
        session.setAttribute("order1",or);
        return "order";
    }
    @RequestMapping("/getAllOrderByUIdByPage")
    public String getAllOrders(@RequestParam(defaultValue = "1") Integer currentPage,
                               @RequestParam(defaultValue = "8") Integer pageSize,
                               HttpServletRequest request,
                               HttpSession session){
        System.out.println("------------getAllOrderByUIdByPage--------------");
        System.out.println("current:"+currentPage);
        User user =(User) session.getAttribute("user1");
        //创建pages对象
        Pages pages=new Pages();
        //设置当前页
        pages.setCurrentPage(currentPage);
        //设置页面容量
        pages.setPageSize(pageSize);
        //查询总记录数
        int total=ordersService.getAllOrderByUIdCount(user.getUid());
        //设置总记录数
        pages.setTotalCount(total);
        //将pages对象保存到request中
        request.setAttribute("pages",pages);
        //调用service，获取查询结果
        List<Order> list = ordersService.getAllOrderByUIdByPage(pages.getCp(),pageSize,user.getUid());
        //将集合保存到request中
        session.setAttribute("order1",list);
        return "order";
    }
//    修改订单状态ostate
    @RequestMapping("/updateOrderState")
    public String updateOrderState(Integer oid){
        Order order = new Order();
        order.setOid(oid);
        order.setOstate(3);
        ordersService.updateOrderState(order);
        return "redirect:/order/getAllOrderByUIdByPage";
    }
//    删除订单
    @RequestMapping("/delOrder")
    public String delOrder(Integer oid,HttpSession session){
        System.out.println("oid:"+oid);
        User u =(User) session.getAttribute("user1");
        List<Order> or = ordersService.getAllOrderByUId(u.getUid());
        for (Order i:or) {
            System.out.println(i);
            if (i.getOid().equals(oid)) {
                System.out.println("进来："+oid);
                i.setOstate(1);
                ordersService.updateOrderState(i);
//                回款
                u.setUcount((Integer)u.getUcount()+(Integer) i.getTicket().getTmoney()*i.getOquantity());
                ordersService.updateUcount(u);
//                加单
                Integer a1 = i.getTicket().getTnum();
                System.out.println("加单1："+a1);
                Integer a2 = i.getOquantity();
                System.out.println("加单2："+a1);
                i.getTicket().setTnum(a1+a2);
                System.out.println("加单3："+i.getTicket());

                ordersService.updateTNum(i.getTicket());
                session.setAttribute("user1",userService.getUserById(u.getUid()));
            }
        }
        return "redirect:/order/getAllOrderByUIdByPage";
    }

}
